package org.lmind.rom.util;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.commons.io.input.SwappedDataInputStream;

public class CentralFileHeader {
	
	public static final int SIGNAL = 0x02014b50;
	
	private int versionMade;
	
	private int version;
	
	private int flag;
	
	private int compressionMethod;
	
	private Date lastModify;
	
	private int crc32;
	
	private long compressedSize;
	
	private long uncompressedSize;
	
	private int nameLength;
	
	private String fileName;
	
	private int commentLength;

	private String comment;
	
	private int extraFieldLength;
	
	private byte[] extraField;
	
	private int internalAttributes;
	
	private int externalAttributes;
	
	private int relativeOffsetOfLocalHeader;

	public int getVersionMade() {
		return versionMade;
	}

	public int getVersion() {
		return version;
	}

	public int getFlag() {
		return flag;
	}

	public int getCompressionMethod() {
		return compressionMethod;
	}

	public Date getLastModify() {
		return lastModify;
	}

	public int getCrc32() {
		return crc32;
	}

	public long getCompressedSize() {
		return compressedSize;
	}

	public long getUncompressedSize() {
		return uncompressedSize;
	}

	public int getNameLength() {
		return nameLength;
	}

	public String getFileName() {
		return fileName;
	}

	public int getCommentLength() {
		return commentLength;
	}

	public String getComment() {
		return comment;
	}

	public int getExtraFieldLength() {
		return extraFieldLength;
	}

	public byte[] getExtraField() {
		return extraField;
	}

	public int getInternalAttributes() {
		return internalAttributes;
	}

	public int getExternalAttributes() {
		return externalAttributes;
	}

	public int getRelativeOffsetOfLocalHeader() {
		return relativeOffsetOfLocalHeader;
	}

	public static CentralFileHeader read(InputStream in) throws EOFException, IOException {
		CentralFileHeader header = new CentralFileHeader();
		SwappedDataInputStream input = new SwappedDataInputStream(in);
		
		int signal = input.readInt();
		if (signal != SIGNAL)
			throw new IOException();
		header.versionMade = input.readUnsignedShort();
		header.version = input.readUnsignedShort();
		header.flag = input.readUnsignedShort();
		header.compressionMethod = input.readUnsignedShort();
		int fileTime = input.readUnsignedShort();
		int fileDate = input.readUnsignedShort();
		header.crc32 = input.readInt();
		header.compressedSize = input.readInt();
		header.uncompressedSize = input.readInt();
		header.nameLength = input.readUnsignedShort();
		header.extraFieldLength = input.readUnsignedShort();
		int fileCommentLength = input.readUnsignedShort();
		int diskNumberStart = input.readUnsignedShort();
		header.internalAttributes = input.readUnsignedShort();
		header.externalAttributes = input.readInt();
		header.relativeOffsetOfLocalHeader = input.readInt();
		
		byte[] fileNameData = new byte[header.nameLength];
		input.readFully(fileNameData);
		header.fileName = new String(fileNameData, "ascii");
		header.extraField = new byte[header.extraFieldLength];
		input.readFully(header.extraField);
		byte[] commentData = new byte[fileCommentLength];
		header.comment = new String(commentData, "ascii");
		
		return header;
	}
}
